/*
 * @Author: lihaogn
 * @Date: 2021-12-11 23:39:00
 * @LastEditTime: 2022-05-27 14:14:37
 * @LastEditor: lihaogn
 * @Description: 用户模型
 * @FilePath: \lix-admin-server\app\model\user.js
 */

"use strict";
const { getUsername } = require("../utils/commonUtils");
module.exports = (app) => {
  const { STRING, TINYINT, DATE } = app.Sequelize;

  // app.model 相当于 sequelize 实例
  const User = app.model.define(
    "user",
    {
      id: { type: STRING(40), primaryKey: true },
      name: {
        type: STRING(20),
        allowNull: false,
        defaultValue: getUsername(),
        unique: "uk_user_name",
      },
      realname: {
        type: STRING(20),
      },
      password: {
        type: STRING(40),
        allowNull: false,
        defaultValue: "123456",
      },
      mobile: { type: STRING(15) },
      email: { type: STRING(255) },
      // 1-启用，2-停用，3-注销
      status: { type: TINYINT, allowNull: false },
      createdAt: { type: DATE, field: "created_at" },
      updatedAt: { type: "TIMESTAMP", field: "updated_at" },
    },
    {
      // 指定数据库中表的名字
      tableName: "users",
    }
  );

  User.associate = function () {
    app.model.User.belongsToMany(app.model.Role, {
      as: "role",
      through: "UserRole",
    });
  };
  return User;
};
